<?php

class Admin_Model_Feature extends Zend_Db_Table_Abstract {

    protected $_name = 'feature';
    protected $_primary = 'id';
    protected $_dependentTables = array('Admin_Model_CategoryFeature', 'Admin_Model_FeatureDetail');
    
    public function getFeature($params = null, $options = null) {
        if ($options == null) {
            $select = $this->select()
                    ->setIntegrityCheck(false)
                    ->from('feature AS f')
                    ->join('category_feature AS cf', 'cf.featureId = f.id', null)
                    ->join('category AS c', 'c.id = cf.categoryId', array('c.id as categoryId, GROUP_CONCAT(c.titleVi) AS categorys'))
            		->where('f.isDelete = 0')
		            ->where('c.isDelete = 0')
            		->group('f.id');
        } elseif ($options['task'] == 'admin-category') {
            $select = $this->select()
		            ->setIntegrityCheck(false)
		            ->from('feature AS f')
		            ->join('category_feature AS cf', 'cf.featureId = f.id', null)
		            ->join('category AS c', 'c.id = cf.categoryId', array('c.id as categoryId, GROUP_CONCAT(c.titleVi) AS categorys'))
                    ->where('c.id = ?', (int) $params['category'])
                    ->where('f.isDelete = 0')
                    ->where('c.isDelete = 0')
            		->group('f.id');
        }
        
        $result = $this->fetchAll($select);
        if ($result) {
            return $result->toArray();
        } else {
            return null;
        }
    }

    public function getFetchPairs($params = null, $options = null) {
        if ($options == null) {
            $query = "SELECT id, titleVi
                        FROM $this->_name";
        } elseif ($options['task'] == 'by-category') {
            $categoryId = $params['categoryId'];
            $query = "SELECT id, titleVi
                        FROM {$this->_name} AS f
                        JOIN category_feature AS cf ON cf.featureId = f.id
                        JOIN category AS c ON c.id = cf.categoryId
                        WHERE f.isDelete = 0 AND c.isDelete = 0 AND c.id = {$categoryId}";
        }
        return $this->_db->fetchPairs($query);
    }

    public function getFeatureOne($id, $options = null) {
        if ($options == null) {
            $select = $this->select()
            		->where('isDelete = 0')
            		->where('id = ?', (int) $id);
        } elseif ($options['task'] == 'category') {
            $select = $this->select()
                    ->setIntegrityCheck(false)
                    ->from("{$this->_name} AS f")
                    ->join('category_feature AS cf', 'cf.featureId = f.id', array('cf.id AS categoryFeatureId'))
                    ->join('category AS c', 'c.id = cf.categoryId', array('GROUP_CONCAT(c.id, ":", c.titleVi) AS category'))
                    ->where('f.isDelete = 0')
                    ->where('c.isDelete = 0')
                    ->where('f.id = ?', (int) $id);
        }
       
        $result = $this->fetchRow($select);
        
        if ($result) {
            return $result->toArray();
        } else {
            return null;
        }
    }

   
    public function saveItem($arrParam = null, $options = null) {
        if ($options['task'] == 'admin-add') {
            $result = $this->insert($arrParam);
        }elseif($options['task'] == 'admin-edit'){
            $result = $this->update($arrParam, "id=" . (int) $arrParam['id']);
        }
        
        return $result;
    }

    public function addItem($info) {
    	try {
        	$id = $this->insert($info);

	        return $id;
        } catch (Exception $e) {
        	throw $e;
        }
    }

    public function updateItem($id, $info) {
    	try {
        	$result = $this->update($info, "id=" . (int) $id);
        	
	        return $result;
        } catch (Exception $e) {
        	throw $e;
        }
    }
    
    /**
     * remove update column isDeleted = 1
     *
     * @param string $params
     * @param string $options
     * @return boolean
     */
    public function removeItem($params = null, $options = null) {
    	try {
    		$data = array('isDelete' => 1);
    		if($options == null){
    			$result = $this->updateItem($params, $data);
    		}elseif($options['task'] == 'remove-select'){
    			foreach($params as $key => $val){
    				$result = $this->updateItem($val, $data);
    			}
    		}elseif($options['task'] == 'remove-all'){
    			$params = $this->getCategorys();
    			 
    			foreach($params as $key => $val){
    				$result = $this->updateItem($val['id'], $data);
    			}
    		}
    	  
    		return $result;
    	} catch (Exception $e) {
    		throw $e;
    	}
    }
    
    /**
     * restore update column isDeleted = 0
     *
     * @param string $params
     * @param string $options
     * @return boolean
     */
    public function restoreItem($params = null, $options = null) {
    	try {
    		$data = array('isDelete' => 0);
    
    		if($options == null){
    			$result = $this->updateItem($params, $data);
    		}elseif($options['task'] == 'restore-select'){
    			foreach($params as $key => $val){
    				$result = $this->updateItem($val, $data);
    			}
    		}elseif($options['task'] == 'restore-all'){
    			$params = $this->getFeatureDetail();
    			 
    			foreach($params as $key => $val){
    				$result = $this->updateItem($val, $data);
    			}
    		}
    			
    		return $result;
    	} catch (Exception $e) {
    		throw $e;
    	}
    }

    public function deleteItem($params = null, $options = null) {
    	try {
    		if($options == null){
    			$featureRowset = $this->find($params);
				$feature = $featureRowset->current();			
				$result = $feature->delete();	
    		}elseif($options['task'] == 'delete-select'){
    			foreach($params as $key => $val){
    				$featureRowset = $this->find($params);
					$feature = $featureRowset->current();			
					$result = $feature->delete();	
    			}
    		}elseif($options['task'] == 'delete-all'){
    			$params = $this->getFeature();
    
    			foreach($params as $key => $val){
    				$featureRowset = $this->find($params);
					$feature = $featureRowset->current();			
					$result = $feature->delete();	
    			}
    		}
    			
    		return $result;
    	} catch (Exception $e) {
    		throw $e;
    	}
    }
    
    public function countItems() {
        $query = $this->select()
                ->from($this->_name, 'count(id) as total');
        $result = $this->fetchRow($query);
        if ($result) {
            return $result->total;
        } else {
            return null;
        }
    }

}